package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.serachZsByInfoDao;
import com.clqteam.model.ZsProduct;
import com.clqteam.util.JDBCutil;

public class serachZsByInfoDaoImpl implements serachZsByInfoDao{

	@Override
	public int serachProductCountByInfo(String searType, String sertInfo) {
		int n = 0;
		StringBuffer sql = new StringBuffer();

		sql.append(" SELECT COUNT(s_zs.zsId) AS pcount");
		sql.append(" FROM s_zs WHERE ");
		sql.append(searType);
		sql.append(" LIKE ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
	
			pst.setString(1, "%"+sertInfo+"%");
			ResultSet rst = pst.executeQuery();
			if (rst.next()) {
				n = rst.getInt("pcount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return n;
	}

	@Override
	public List<ZsProduct> serachProductByInfo(String searType, String sertInfo, int page, int rows) {
		List<ZsProduct> zsps = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM s_zs WHERE ");
		sql.append(searType);
		sql.append(" LIKE ?");
		sql.append(" ORDER BY s_zs.zsId");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, "%"+sertInfo+"%");
			pst.setInt(2, (page - 1) * rows);
			pst.setInt(3, rows);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				ZsProduct z = new ZsProduct();
				z.setZspId(rst.getInt("zspId"));
				z.setZspName(rst.getString("zspName"));
				z.setZspAmount(rst.getInt("zspAmount"));
				z.setZspCost(rst.getDouble("zspCost"));
				z.setZspTime(rst.getString("zspTime"));
				z.setZspPeople(rst.getString("zspPeople"));
				z.setZsReason(rst.getString("zsReason"));
				z.setZsId(rst.getInt("zsId"));
				zsps.add(z);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return zsps;
	}

}
